package com.kuangxiaoyang.foodapp.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.foodclient.R;
import com.example.foodclient.pojo.FoodData;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodListAdapter extends BaseAdapter {
    private Context context;
    private List<FoodData.DataBean> foodList;

    public FoodListAdapter(Context context, List<FoodData.DataBean> foodList) {
        this.context = context;
        this.foodList = foodList;
    }

    @Override
    public int getCount() {
        return foodList.size();
    }

    @Override
    public Object getItem(int position) {
        return foodList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(context, R.layout.food_item, null);

        TextView foodNameTv = convertView.findViewById(R.id.food_name_tv);
        TextView foodBriefTv = convertView.findViewById(R.id.food_brief_tv);
        TextView foodPriceTv = convertView.findViewById(R.id.food_price_tv);
        ImageView foodImg = convertView.findViewById(R.id.food_img);

        foodNameTv.setText(foodList.get(position).getName());
        foodBriefTv.setText(foodList.get(position).getLabel());
        foodPriceTv.setText("价格: " + String.valueOf(foodList.get(position).getPrice()) + "元");

        String imgName = foodList.get(position).getIcon();

//        int imgId = context.getResources().getIdentifier(imgName, "mipmap", context.getPackageName());
//        foodImg.setImageResource(imgId);

        Picasso.with(context).load("http://192.168.137.1:8083/img/" + imgName).into(foodImg);

        return convertView;
    }
}
